package temp.lho.lws.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import temp.lho.lws.mapper.IBaseMapper;
import temp.lho.lws.mapper.IMenuMapper;
import temp.lho.lws.pojo.TbMenu;
import temp.lho.lws.service.IMenuService;
import temp.lho.lws.vo.MenuInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuws
 * @create 2020-06-27 17:19
 */
@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<TbMenu> implements IMenuService {

    @Autowired
    private IMenuMapper menuMapper;

    @Override
    public List<MenuInfo> findUserMenu(Integer userId) {
        //查询用户所有菜单
        List<TbMenu> userMenu = menuMapper.findUserMenu(userId);
        //创建指定格式返回给前端的菜单对象
        List<MenuInfo> menuInfos = new ArrayList<MenuInfo>(3);

        // 头部菜单
        MenuInfo menuInfo = null;
        // 侧边一级菜单
        MenuInfo menuSide = null;
        // 侧边二级菜单
        MenuInfo menuChild = null;
        for (TbMenu menu : userMenu) {
            //头部一级菜单
            if (menu.getMenuParentId() == 0) {
                menuInfo = new MenuInfo();
                menuInfo.setTitle(menu.getMenuName());
                menuInfo.setHref(menu.getMenuViewpath());
                menuInfo.setIcon(menu.getMenuIcon());

                // 侧边一级菜单
                List<MenuInfo> menuSides = new ArrayList<>(5);
                for (TbMenu menu2 : userMenu) {
                    if (menu.getMenuId() == menu2.getMenuParentId()) {
                        menuSide = new MenuInfo();
                        menuSide.setTitle(menu2.getMenuName());
                        menuSide.setHref(menu2.getMenuViewpath());
                        menuSide.setIcon(menu2.getMenuIcon());

                        // 侧边二级子菜单
                        List<MenuInfo> menuChilds = new ArrayList<>(5);
                        for (TbMenu menu3 : userMenu) {
                            if (menu2.getMenuId() == menu3.getMenuParentId()) {
                                menuChild = new MenuInfo();
                                menuChild.setTitle(menu3.getMenuName());
                                menuChild.setHref(menu3.getMenuViewpath());
                                menuChild.setIcon(menu3.getMenuIcon());
                                menuChilds.add(menuChild);
                            }
                        }
                        menuSide.setChild(menuChilds);
                        menuSides.add(menuSide);
                    }
                }
                menuInfo.setChild(menuSides);
                menuInfos.add(menuInfo);
            }
        }
        return menuInfos;
    }

    @Override
    protected IBaseMapper<TbMenu> baseMapper() {
        return menuMapper;
    }
}
